package com.lezijie.note.dao;

import com.lezijie.note.po.User;
import com.lezijie.note.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDao {
    public User queryUser(String name){
        User user=null;
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        System.out.println(name);
        try {
//            获取数据库连接
            connection=DBUtil.getConnection();
//            定义sql
            String sql="select * from tb_user where uname=?";
//            预编译
            preparedStatement=connection.prepareStatement(sql);
//            设置参数
            preparedStatement.setString(1,name);
//            执行查询 获取返回结果
            resultSet=preparedStatement.executeQuery();
//            判断并且分析结果
            if(resultSet.next()){
                user =new User();
                user.setUname(name);
                user.setHead(resultSet.getString("head"));
                user.setUserId(resultSet.getInt("userid"));
                user.setMood(resultSet.getString("mood"));
                user.setUpwd(resultSet.getString("upwd"));
                user.setNick(resultSet.getString("nick"));
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtil.close(resultSet,preparedStatement,connection);
        }
        return  user;
    }
}
